package com.liaoban.app.ui.rank;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.liaoban.app.R;
import com.liaoban.app.adapter.RankChildAdapter;
import com.liaoban.app.adapter.RankChildTopAdapter;
import com.liaoban.app.api.type.Rank;
import com.liaoban.app.api.type.request.RankRequest;
import com.liaoban.app.api.type.request.WuserinfoRequest;
import com.liaoban.app.biz.CommonBiz;
import com.liaoban.app.constants.Constant;
import com.liaoban.app.constants.ExtraParam;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.anchor.AnchorDetailActivity;
import com.liaoban.app.ui.base.BaseListFragment;
import com.liaoban.app.ui.login.LoginActivity;
import com.liaoban.app.widget.MyGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by ybc on 2018/7/13 0013.
 */
public class RankChildFragment extends BaseListFragment<Rank> {
    @BindView(R.id.rv_top)
    RecyclerView rvTop;

    private String mType;
    private RankChildTopAdapter mRankChildTopAdapter;

    public static Bundle getBundle(String type) {
        Bundle bundle = new Bundle();
        bundle.putString(ExtraParam.TYPE, type);
        return bundle;
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_rank_child;
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        return new RankChildAdapter(null);
    }

    @Override
    public void itemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        Rank rank = (Rank) baseQuickAdapter.getData().get(position);
        switch (view.getId()) {
            case R.id.iv_follow:
                showRunningDialog(R.string.committing);
                WuserinfoRequest wuserinfoRequest = new WuserinfoRequest();
                wuserinfoRequest.setWuserId(rank.getWuserId() + "");
                startTask(CommonBiz.getInstance().follow(wuserinfoRequest), baseResponse -> {
                    rank.setFansIsStatus(baseResponse.code);
                    ToastHelper.show(getActivity(), baseResponse.msg);
                    baseQuickAdapter.notifyItemChanged(position);
                });
                break;
        }
    }

    @Override
    public void itemClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
        if (!CommonBiz.getInstance().isLogin(getActivity())) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        Rank rank = (Rank) baseQuickAdapter.getData().get(position);
        startActivity(AnchorDetailActivity.newIntent(getActivity(), String.valueOf(rank.getWuserId())));
    }

    @Override
    protected void initView(View contentView) {
        initData();
        initViewRvTop();
        super.initView(contentView);
    }

    private void initData() {
        Bundle bundle = getArguments();//从activity传过来的Bundle
        if (bundle != null) {
            mType = bundle.getString(ExtraParam.TYPE);
        }
    }

    private void initViewRvTop() {
        MyGridLayoutManager manager = new MyGridLayoutManager(getActivity(), 3);
        rvTop.setLayoutManager(manager);
        mRankChildTopAdapter = new RankChildTopAdapter(null);
        rvTop.setAdapter(mRankChildTopAdapter);
        mRankChildTopAdapter.setOnItemClickListener(this::itemClick);
        mRankChildTopAdapter.setOnItemChildClickListener(this::itemChildClick);
    }

    @Override
    public void getData() {
        RankRequest rankRequest = new RankRequest();
        rankRequest.setListRows("" + Constant.PAGE_SIZE);
        rankRequest.setPage("" + getPageIndex());
        rankRequest.setType(mType);
        startTask(CommonBiz.getInstance().bandList(rankRequest), listBaseResponse -> {
            List<Rank> datas = listBaseResponse.data;
            if (getPageIndex() == 1) {
                List<Rank> topDatas = new ArrayList<>();
                List<Rank> bottomDatas = new ArrayList<>();
                for (int i = 0; i < datas.size(); i++) {
                    if (i <= 2) { //前三条放到顶部
                        topDatas.add(datas.get(i));
                    } else {
                        bottomDatas.add(datas.get(i));
                    }
                }
                mRankChildTopAdapter.setNewData(topDatas);
                doSuc(bottomDatas, 1);
            } else {
                doSuc(datas, 1);
            }
        });
    }
}
